﻿using System;
using System.Web;
using System.Configuration;
using System.Collections.Generic;

namespace KKCai.Html
{
    internal class Rule
    {
        public string LookFor { get; set; }

        public string SendTo { get; set; }

        public int TimeSpan { get; set; }
    }

    internal class RuleManager
    {
        private const string KEY = "aspx2html";

        public static List<Rule> GetRules()
        {
            if (HttpContext.Current.Cache[KEY] == null)
                HttpContext.Current.Cache.Insert(KEY, ConfigurationManager.GetSection(KEY));

            return HttpContext.Current.Cache[KEY] as List<Rule>;
        }

        public static string ResolveUrl(string appPath, string url)
        {
            if (url.Length == 0 || url[0] != '~')
                return url;
            else
            {
                if (url.Length == 1)
                    return appPath;
                if (url[1] == '/' || url[1] == '\\')
                {
                    if (appPath.Length > 1)
                        return String.Format("{0}/{1}", appPath, url.Substring(2));
                    else
                        return String.Format("/{0}", url.Substring(2));
                }
                else
                {
                    if (appPath.Length > 1)
                        return String.Format("{0}/{1}", appPath, url.Substring(1));
                    else
                        return appPath + url.Substring(1);
                }
            }
        }
    }
}
